﻿using System;
using System.Collections.Generic;
using Best.SmartCms.Dao;
using Best.SmartCms.Domain;
using Best.SmartCms.Domain.Security;
using Best.SmartCms.Dto.Response;
using Best.SmartCms.Dto.User;
using Best.SmartCms.Service.BizException;
using Best.SmartCms.Service.Common;
using Best.SmartCms.Utility;

namespace Best.SmartCms.Service.Security
{
    /// <summary>
    /// 用户服务
    /// </summary>
    public class UserService : IService
    {
        private readonly UserDao _userDao;
        private readonly WorkContext _workContext;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mySqlDbContext"></param>
        /// <param name="workContext"></param>
        public UserService(MySqlDbContext mySqlDbContext, WorkContext workContext)
        {
            _workContext = workContext;
            _userDao = new UserDao(mySqlDbContext);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public User FindById(long id)
        {
            return _userDao.FindById(id);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public User FindByPhone(string phone)
        {
            return _userDao.FindEnabledUserByPhone(phone);
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagedResponse<UserListDto> PageList(UserListQueryDto query)
        {
            var list = _userDao.PageList(query, out var totalCount, out var totalPages);
            var pageList = TinyMapperExtensions.Map<List<User>, List<UserListDto>>(list);
            return new PagedResponse<UserListDto>(pageList, query.PageIndex,query.PageSize,totalCount,totalPages);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        public void Create(UserEditDto dto)
        {
            var entity = TinyMapperExtensions.Map<UserEditDto, User>(dto);
            var randomHelper=new RandomHelper();
            entity.PasswordSalt = randomHelper.CreateRandomString(6, true);
            entity.Password = Md5Utility.Encrypt(dto.Password, entity.PasswordSalt);
            entity.CreatedTime = DateTime.Now;
            entity.CreatorId = _workContext.UserId;
            entity.CreatorName = _workContext.UserName;
            entity.UpdatorId = _workContext.UserId;
            entity.UpdatorName = _workContext.UserName;
            entity.UpdatedTime = DateTime.Now;
            _userDao.Add(entity);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        public void Edit(UserEditDto dto)
        {
            var entity = _userDao.GetSingle(dto.Id);
            if (entity == null)
            {
                throw new ParameterNullException($"未找到{dto.Id}的数据");
            }

            TinyMapperExtensions.Map(dto, entity);
            entity.Password = Md5Utility.Encrypt(dto.Password, entity.PasswordSalt);
            
            entity.UpdatorId = _workContext.UserId;
            entity.UpdatorName = _workContext.UserName;
            entity.UpdatedTime = DateTime.Now;
            _userDao.Update(entity);
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserEditDto Info(long id)
        {
            var entity = _userDao.GetSingle(id);
            entity.PasswordSalt = "";
            entity.Password = "";
            return TinyMapperExtensions.Map<User, UserEditDto>(entity);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(long id)
        {
            var entity = _userDao.GetSingle(id);
            if (entity == null)
            {
                throw new ParameterNullException($"未找到{id}的数据");
            }

            _userDao.Delete(entity);
        }
    }
}
